|
|
|
|
|
|
|
The code in Listing 5.20 combines code from several of the KeyPress events you coded in the first part of this chapter. The only real difference is the use of the RaiseEvent keyword. This keyword is used to send a message back to the host program using this control. In this case, the messages are sent if an invalid key has been entered. Along with the message, the invalid key's ASCII value is returned. You see this event when you test the control a bit later in this chapter. After adding the KeyValidate routine, you need to add some code to the project to call KeyValidate each time a key is pressed. Listing 5.21 shows the code to add to the Text1_KeyPress event. |
|
|
|
|
|
|
|
|
Listing 5.21. Coding the Text1_KeyPress event. |
|
|
|
|
|
|
|
|
Private Sub Text1_KeyPress(KeyAscii As Integer)
'
KeyAscii = KeyValidate(KeyAscii)
'
End Sub |
|
|
|
|
|
|
|
|
This code should look familiar. Now, each time the user presses a key in the textbox, the KeyValidate function is called. If the key press is invalid, an error event message is fired off to the host program. |
|
|
|
|
|
|
|
|
That's all the coding you need to do to create a custom control that filters key presses. Save the UserControl module (VTEXT.CTL) and the project file (CUSTOMVALIDATION.VBP) and close all the windows associated with this project. You should now see a new control appear in the toolbox. This is your new custom control. You use it in a test form to make sure you've built all the code correctly. |
|
|
|
|
|
|
|
|
Testing the KeyFilter Options of the VText Control |
|
|
|
|
|
|
|
|
To test the new control, you need to add a new project to this project group. Select File | Add Project and select the Standard EXE project template from the New Project tab. Locate the new VText control in your toolbox and add it to the blank form (see Figure 5.13). |
|
|
|
|
|
|
|
|
Figure 5.13.
Adding the VText
control to a blank form. |
|
|
|
|
|